package com.hugo.maomi_first.home.view;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.hugo.maomi_first.R;
import com.hugo.maomi_first.adapter.HomeGoodsAdapter;
import com.hugo.maomi_first.base.BaseFragment;
import com.hugo.maomi_first.bean.HomeGoodsBean;
import com.hugo.maomi_first.bean.HomeHeaderBean;
import com.hugo.maomi_first.home.presenter.HomePresenterImpl;
import com.hugo.maomi_first.home.presenter.IHomePresenter;
import com.hugo.maomi_first.utils.ImageLoader;
import com.hugo.maomi_first.utils.NetworkImageHolderView;
import com.hugo.maomi_first.widget.ScrollViewWithListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;


public class HomeFragment extends BaseFragment implements IHomeView,OnItemClickListener {

    private IHomePresenter presenter;
    @Bind(R.id.listView_home)
    ScrollViewWithListView listView_home;
    @Bind(R.id.convenient_banner_home)
    ConvenientBanner convenient_banner_home;
    @Bind(R.id.brands_title_big)
    TextView brands_title_big;
    @Bind(R.id.brands_title_sml)
    TextView brands_title_sml;
    @Bind(R.id.iv_brands_icon)
    ImageView ivBrandsIcon;
    @Bind(R.id.tv_brands_discount)
    TextView tvBrandsDiscount;
    @Bind(R.id.tv_brands_title)
    TextView tvBrandsTitle;


    private HomeGoodsAdapter adapter;

    public HomeFragment() {
        // Required empty public constructor
    }

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView() {
        presenter = new HomePresenterImpl(this);
        presenter.loadHomeHeader();
        presenter.loadHomeGoods();
    }


    @Override
    public void loadHomeHeader(HomeHeaderBean.DataBean bean) {

        List<HomeHeaderBean.DataBean.TopicsBean> topicsBeanList = bean.getTopics();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < topicsBeanList.size(); i++) {
            list.add(topicsBeanList.get(i).getImg().getImg_url());
        }
        //自定义你的Holder，实现更多复杂的界面，不一定是图片翻页，其他任何控件翻页亦可。
        convenient_banner_home.setPages(
                new CBViewHolderCreator<NetworkImageHolderView>() {
                    @Override
                    public NetworkImageHolderView createHolder() {
                        return new NetworkImageHolderView();
                    }
                }, list)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable
                        .ic_page_indicator_focused})
                        //设置指示器的方向
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                .setOnItemClickListener(this);
        //品牌特卖
        //为brands_title_big填充数据
        brands_title_big.setText(bean.getBrands_title_big());
        //为brands_title_sml填充数据
        brands_title_sml.setText(bean.getBrands_title_sml());

        //红标签
        String imgUrl = bean.getBrands().get(0).getImg().getImg_url();
        ImageLoader.getInstance().displayImage(context, imgUrl, ivBrandsIcon);
        String discount = bean.getBrands().get(0).getDiscount();
        tvBrandsDiscount.setText(discount);
        String title = bean.getBrands().get(0).getTitle();
        tvBrandsTitle.setText(title);

    }

    @Override
    public void loadHomeGoods(HomeGoodsBean bean) {
        List<HomeGoodsBean.DataBean.ItemsBean> itemsBeanList = bean.getData().getItems();
        adapter = new HomeGoodsAdapter(context,itemsBeanList);
        listView_home.setAdapter(adapter);


    }

    @Override
    public void onItemClick(int position) {
        Toast.makeText(getContext(), "点击了第" + position + "个", Toast.LENGTH_SHORT).show();

    }

    //开始翻页
    @Override
    public void onResume() {
        super.onResume();
        //开始自动翻页
        convenient_banner_home.startTurning(3000);
    }

    // 停止自动翻页
    @Override
    public void onPause() {
        super.onPause();
        //停止翻页
        convenient_banner_home.stopTurning();
    }


}
